import requests
from bs4 import BeautifulSoup
from lxml import etree
import pprint


class Mygym:

    def __init__(self):
        self.url = 'http://www.mygymchina.com/family/img.php?lang=cn&class1=118'
        self.header = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
        }

    #获取源码
    def get_code(self,url):
        ret = requests.get(url,headers=self.header)
        return ret.content.decode('utf-8')

    #获取城市url
    def get_city_list(self,data):
        xpath_data = etree.HTML(data)
        city_url = xpath_data.xpath('//div[@class="qgzx_nr"]//ul/li//a/@href')
        city_text = xpath_data.xpath('//div[@class="qgzx_nr"]//ul/li//a/text()')
        city_list = []
        for url in city_url:
            replace_url = url.replace('..','http://www.mygymchina.com')
            city_list.append(replace_url)

        for i in city_list:
            r = requests.get(i,headers=self.header)
            core_data = etree.HTML(r.content.decode('utf-8'))
            res = core_data.xpath('//div[@class="qgzx"]//tr//td//a/@href')
            if res:
                core_url_list = []
                list_set= list(set(res))
                for core_url in list_set:
                    core_url = 'http://www.mygymchina.com/family/' + core_url
                    core_url_list.append(core_url)

                list_detailed = []
                dic_detailed_date = {}
                for num_url in core_url_list:
                    dic_detailed = {}
                    num = requests.get(num_url, headers=self.header)
                    num_date = etree.HTML(num.content.decode('utf-8'))
                    core_name = num_date.xpath('//div[@class="zxnrbt"]/text()')  #中心名称
                    detailed_data = num_date.xpath('//div[@class="zxlxfs"]/text()')  #中心信息
                    dic_detailed['中心名称'] = core_name[0]
                    dic_detailed['中心电话'] = detailed_data[0]
                    dic_detailed['中心地址'] = detailed_data[-1]
                    list_detailed.append(dic_detailed)
                # dic_detailed_date['city'] = city_text
                dic_detailed_date['detailed_information'] = list_detailed
                print(dic_detailed_date)
            else:
                continue

m = Mygym()
ret = m.get_code('http://www.mygymchina.com/family/img.php?lang=cn&class1=118')
m.get_city_list(ret)